package http

import (
	"gitee.com/Frozentear/layout/docs"
	"gitee.com/Frozentear/layout/internal/conf"
	"gitee.com/bethink1501/24on-library/ginmiddle"

	"github.com/gin-gonic/gin"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/logging"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/transport/http"
	"github.com/google/wire"
	"github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"

)

var ProviderHTTP = wire.NewSet(NewHTTPServer, NewUserRest)

func NewHTTPServer(
	logger log.Logger,
	cfg *conf.Server,
	userRest *UserRest,

) (*http.Server, error) {

	opts := []http.ServerOption{
		http.Middleware(
			recovery.Recovery(),
			logging.Server(logger),
		),
	}

	if cfg.Http.Addr != "" {
		opts = append(opts, http.Address(cfg.Http.Addr))
	}

	if cfg.Http.Timeout.Duration() != 0 {
		opts = append(opts, http.Timeout(cfg.Http.Timeout.Duration()))
	}

	server := http.NewServer(opts...)

	engine := gin.Default()
	engine.Use(ginmiddle.StartTimeMiddle(), ginmiddle.CORSMiddleware())

	group := engine.Group("user-center")

	docs.SwaggerInfo.BasePath = "/" + "user-center"
	group.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.PersistAuthorization(true)))
	server.HandlePrefix("/", engine)

	return server, nil
}
